/*  
cd /projects/hsieh_project/proj_201809/code_1_data/
qsas data_1_cityind_sum_msa_czone.sas 5 & 
*/

libname hr "/projects/hsieh_project/proj_201809/data/";

%Let dir_out = /projects/hsieh_project/proj_201809/data/;

/*---------------------------------------------------------*/
/* Load Raw Data */
/*---------------------------------------------------------*/

%include "/projects/hsieh_project/proj_201809/code_1_data/m_read.sas" /source2;
%m_read(param_dev=1,param_lyear=0, param_czone=1, param_msa1983=1, param_msacz=1, param_drop=1);

data lbd_init;
  set lbd;
run;

/*
--------------------------------------------------------------------------------
Macros to Aggreate to desired level
*/

%include "/projects/hsieh_project/proj_201809/code_1_data/data_0_cityind_sum_core.sas" /source2;

%macro m_main(vgeo= );

%put --------------------------------------------------------------------------------;
%put Geo Variable: &vgeo.;

%cind_sum(dt_in=lbd, dt_out=lbd_cind_&vgeo., c_city=&vgeo., l_ind_perc=10 5 1, l_cind_rank=1);

data lbd_cind_&vgeo.;
  set lbd_cind_&vgeo.;
  drop _type_ _freq_;
run;

proc export data=lbd_cind_&vgeo. outfile="&dir_out./cityind_sum_&vgeo._all.dta" replace;
run;

%mend;

/*
--------------------------------------------------------------------------------
MSA1983
*/

%m_main(vgeo=msa1983);

/*
--------------------------------------------------------------------------------
Hybrid MSA-CZONE
*/

%m_main(vgeo=msa1983cz);
%m_main(vgeo=msa2010cz);

/*
--------------------------------------------------------------------------------
CZONE with FIPS in MSA
*/

proc import out=cw_geo datafile="/projects/hsieh_project/proj_201809/data/cw_geo_fips_czone_in_msa_v12.dta" replace;
run;

proc sort data=lbd; by fips czone;
proc sort data=cw_geo; by fips czone;

data lbd;
  merge lbd(in=x) cw_geo;
  by fips czone;
  if x = 1;
run;

data lbd;
  set lbd;
  if fips_in_msa = 1;
  rename czone=czone_fips_in_msa;
run;

%m_main(vgeo=czone_fips_in_msa);


/* End of SAS file */